<?php

namespace app\admin\model\user;

use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\store\Store;
use think\Model;


class Yajinlog extends Model
{

    

    

    // 表名
    protected $name = 'user_yajin_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'store_name_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '-1' => __('Status -1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStoreNameTextAttr($value, $data)
    {
        if(isset($data['order_id'])){
            $orderInfo = OrderItem::where(['order_id' => $data['order_id']])->find();
            if($orderInfo){
                $value = Store::where(['id' => $orderInfo['store_id']])->value('name');
            }
        }
        return $value;
    }


    public function order()
    {
        return $this->belongsTo('app\admin\model\shopro\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
